package com.vq.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vq.entity.FriendList;
import com.vq.entity.User;
import com.vq.mapper.FriendListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FriendListService {
    @Autowired
    FriendListMapper friendListMapper;

    @Autowired
    UserService userService;

    /**
     * 添加
     */
    public boolean insert(FriendList friendList) {
        //帮助不会添加自己
        if (friendList.getMyAccount().equals(friendList.getHeAccount())) return false;
        //保证好友双方账户真实存在
        User myUser = userService.selectByAccount(friendList.getMyAccount(), false);
        User heUser = userService.selectByAccount(friendList.getHeAccount(), false);
        if(myUser==null) return false;
        if(heUser==null) return false;
        //好友列表中没有该关系
        if(isEmpty(friendList)) {
            friendListMapper.insert(friendList);//添加好友关系
            userService.appendFollowersNum(myUser.getId());//增加自己的关注量
            userService.appendFansNum(heUser.getId());//增加对方的粉丝量
            return true;
        }
        return false;
    }

    /**
     * 删除好友
     */
    public boolean deleteByAll(FriendList friendList) {
        User myUser = userService.selectByAccount(friendList.getMyAccount(), false);
        User heUser = userService.selectByAccount(friendList.getHeAccount(), false);
        //保证好友双方账户真实存在
        if(myUser==null) return false;
        if(heUser==null) return false;
        userService.reduceFollowersNum(myUser.getId());//减少自己的关注量
        userService.reduceFansNum(heUser.getId());//减少对方的粉丝量
        LambdaQueryWrapper<FriendList> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FriendList::getMyAccount, friendList.getMyAccount()).eq(FriendList::getHeAccount, friendList.getHeAccount());
        return friendListMapper.delete(lqw)==1;
    }

    /**
     * 查询关注列表
     * @param myAccount
     * @return
     */
    public List<User> selectByMyAccount(String myAccount) {
        //获取用户关注列表
        LambdaQueryWrapper<FriendList> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FriendList::getMyAccount, myAccount);
        List<FriendList> friendLists = friendListMapper.selectList(lqw);

        //获取好友信息
        List<User> users = new ArrayList<>();
        for (FriendList friendList : friendLists) {
            //如果不是好友关系，则添加
            if (!isFriend(friendList)) {
                User user = userService.selectByAccount(friendList.getHeAccount(), false);
                users.add(user);
            }
        }
        return users;
    }
    /**
     * 查询粉丝列表
     * @param myAccount
     * @return
     */
    public List<User> selectFansByMyAccount(String myAccount) {
        //获取用户关注列表
        LambdaQueryWrapper<FriendList> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FriendList::getHeAccount, myAccount);
        List<FriendList> friendLists = friendListMapper.selectList(lqw);

        //获取好友信息
        List<User> users = new ArrayList<>();
        for (FriendList friendList : friendLists) {
            //如果不是好友关系，则添加
            if (!isFriend(friendList)) {
                User user = userService.selectByAccount(friendList.getMyAccount(), false);
                users.add(user);
            }
        }
        return users;
    }
    /**
     * 查询所有该用户的好友
     */
    public List<User> selectFriendByMyAccount(String myAccount) {
        //获取用户关注列表
        LambdaQueryWrapper<FriendList> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FriendList::getMyAccount, myAccount);
        List<FriendList> friendLists = friendListMapper.selectList(lqw);

        //获取好友信息
        List<User> users = new ArrayList<>();
        for (FriendList friendList : friendLists) {
            //如果为好友关系，则添加
            if (isFriend(friendList)) {
                User user = userService.selectByAccount(friendList.getHeAccount(), false);
                users.add(user);
            }
        }
        return users;
    }
    /**
     * 查询关系是否存在
     */
    public boolean isEmpty(FriendList fr) {
        LambdaQueryWrapper<FriendList> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FriendList::getMyAccount, fr.getMyAccount()).eq(FriendList::getHeAccount, fr.getHeAccount());
        FriendList friendList = friendListMapper.selectOne(lqw);
        //为空，返回true
        if(friendList == null) {
            return true;
        }
        //不为空，返回false
        else {
            return false;
        }
    }
    /**
     * 查询好友关系是否存在
     * @param fr
     * @return
     */
    public boolean isFriend(FriendList fr) {
        //查询自己是否关注对方
        LambdaQueryWrapper<FriendList> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(FriendList::getMyAccount, fr.getMyAccount()).eq(FriendList::getHeAccount, fr.getHeAccount());
        //查询对方是否关注自己
        LambdaQueryWrapper<FriendList> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(FriendList::getMyAccount, fr.getHeAccount()).eq(FriendList::getHeAccount, fr.getMyAccount());
        //返回是否同时存在
        return friendListMapper.selectOne(lqw1)!=null&&friendListMapper.selectOne(lqw2)!=null;
    }
}
